﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using DTO;
using BUS;


namespace TVC_WEB.Global
{
    public partial class SearchAdvance : System.Web.UI.Page
    {
        String sErr = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            LoadControlSearch();
        }

        protected void LoadControlSearch()
        {


            BUS.CategoriesControl ctr = new BUS.CategoriesControl();
            DataTable dtMenu = ctr.GetRootsCategories("", ref sErr);


            if (dtMenu.Rows.Count > 0)
            {

                for (int i = 0; i < dtMenu.Rows.Count; i++)
                {
                    //kq += "<tr><td class=\"l_form\"><b>";
                    //kq += dtMenu.Rows[i]["NameVN"].ToString().Trim();
                    //kq += "</b> </td><td>";
                    //kq += "<select id=\"radcbb" + dtMenu.Rows[i]["ID"].ToString().Trim() + "\" runat=\"server\">";
                    //kq += LoadSubControl(Convert.ToInt32(dtMenu.Rows[i]["ID"]));
                    //kq += "</select></td></tr>";
                    //kq += "<tr><td class=\"spacer\"></td></tr>";

                    Literal liter1 = new Literal();
                    liter1.Text = "<tr><td class=\"l_form\"><b>";
                    Panel1.Controls.Add(liter1);

                    Literal liter2 = new Literal();
                    liter2.Text = dtMenu.Rows[i]["NameVN"].ToString().Trim() + ":</b></td><td>";
                    Panel1.Controls.Add(liter2);

                    Control temp = new Control();
                    temp = LoadSubControl(Convert.ToInt32(dtMenu.Rows[i]["ID"]));
                    Panel1.Controls.Add(temp);

                    Literal liter3 = new Literal();
                    liter3.Text = "</td></tr><tr><td class=\"spacer\"></td></tr>";
                    Panel1.Controls.Add(liter3);


                    //Label lb = new Label();
                    //lb.Text = dtMenu.Rows[i]["NameVN"].ToString().Trim();
                    //lb.Attributes["style"] = "font-size: 15px;";
                    //Panel1.Controls.Add(lb);





                    //Literal liter = new Literal();
                    //liter.Text = "</br></br>";
                    //Panel1.Controls.Add(liter);
                }

            }

        }

        protected Control LoadSubControl(int id)
        {
            Telerik.Web.UI.RadComboBox radcbb = new Telerik.Web.UI.RadComboBox();

            CategoriesControl ctr = new CategoriesControl();
            DataTable dt = ctr.get_childs(id, ref sErr);

            DataRow row = dt.NewRow();
            row["NameVN"] = "None";
            row["ID"] = "-1";
            dt.Rows.InsertAt(row, 0);

            radcbb.DataSource = dt;
            radcbb.DataValueField = "ID";
            radcbb.DataTextField = "NameVN";
            radcbb.Attributes["style"] = "margin-left: 100px;";
            radcbb.DataBind();

            return radcbb;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            String cate = "";
            String type = "";


            foreach (Control ctr in Panel1.Controls)
            {
                if (ctr is Telerik.Web.UI.RadComboBox)
                {
                    Telerik.Web.UI.RadComboBox radcbb = (Telerik.Web.UI.RadComboBox)ctr;
                    CategoriesControl ctrcate = new CategoriesControl();
                    if (radcbb.SelectedValue.ToString().Trim() != "-1")
                    {
                        CategoriesInfo inf = ctrcate.Get(Convert.ToInt32(radcbb.SelectedValue), ref sErr);
                        cate += inf.ID.ToString() + ",";
                        type += inf.Type.ToString() + ",";
                    }

                }
            }
            if (cate != "" && cate[cate.Length - 1] == ',')
                cate = cate.Remove(cate.Length - 1);
            if (type != "" && type[type.Length - 1] == ',')
                type = type.Remove(type.Length - 1);
            String url = "";
            if (cate != "" && type != "")
                url = "prolist.aspx?cate=" + cate.ToString() + "&catetype=" + type + "&content=" + txtSearch.Text;
            else
                url = "prolist.aspx?content=" + txtSearch.Text;
            Response.Redirect(url);


        }
    }
}
